<template>
	<view class="container">		
		<uni-section title="资产信息" type="line" v-if="form.id">
			<uni-list>
				<uni-list-item title="分类型号" :rightText="form.cate_name + ' ' + form.model_name"/>
				<uni-list-item title="资产编号" :rightText="form.num"/>
				<uni-list-item title="原价值" :rightText="form.price1 + ''"/>
			</uni-list>
		</uni-section>
		<view class="example">
			<uni-forms ref="form" :model="form" labelWidth="80px">
				<uni-forms-item label="盘点状态" name="status" required>
					<uni-data-checkbox v-model="form.status" :localdata="statusOptions" :disabled="action == 'view'"></uni-data-checkbox>
				</uni-forms-item>		
				<uni-forms-item label="当前使用人" name="userId" v-if="form.status == '2'" required>				
					<uni-data-select v-model="form.userId" :localdata="userOptions" :disabled="action == 'view'"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="当前价值" name="price" required>
					<uni-easyinput v-model="form.price" type="number" placeholder="请输入盘点价值" :disabled="action == 'view'"/>
				</uni-forms-item>	
				<uni-forms-item label="归属仓库" name="houseId" required>				
					<uni-data-select v-model="form.houseId" :localdata="houseOptions" :disabled="action == 'view'"></uni-data-select>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="save" v-if="action != 'view'">保存</button>
		</view>
	</view>
</template>

<script>
	import { selectDictLabel, parseTime, getDicts	} from '@/api/sys.js'
	import { apiGet, apiPost, apiPut, apiDs	} from '@/api/openapi'

	export default {
		data() {
			return {
				action: 'edit',
				form: {},
				rules: {
					status: {
						rules: [{
							required: true,
							errorMessage: '盘点不能为空'
						}]
					},
					house_id: {
						rules: [{
							required: true,
							errorMessage: '归属仓库不能为空'
						}]
					},
					price: {
						rules: [{
							required: true,
							errorMessage: '盘点价值不能为空'
						}]
					}
				},
				statusOptions:[],
				userOptions:[],
				houseOptions:[]
			}
		},
		onLoad(option) {
			if(option.action){
				this.action = option.action;
			}
			let params = {"@table":"asset_stock_detail"}
			if(option.assetId){
				params['asset_id'] = option.assetId;
			}else if(option.id){
				params['id'] = option.id;
			}
			apiGet(params).then(response => {
				if(response.data == null){
					this.$modal.msgError("未找到待盘点任务");
					uni.navigateBack({delta:1});					
					return;
				}else{
					this.form = response.data;
					this.init();
				}				
			})		
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			init() {
				apiDs('app_warehouse_select').then(response => {
					this.houseOptions = response.data;
				})
				apiDs('app_user_select').then(response => {
					this.userOptions = response.data;
				})
				getDicts('assets_status').then(response => {
					response.data.forEach(e=>{
						e.value = e.dictValue;
						e.text = e.dictLabel;
					})
					this.statusOptions = response.data;	
				});
			},
			save(ref) {
				this.$refs.form.validate().then(res => {
					let data = {"@tags": ['asset_stock_detail']};
					data['asset_stock_detail'] = this.form;
					apiPut(data).then(response => {
					  this.$modal.msgSuccess("盘点成功");
					});			
				});
			},
			dictLabel(dictData, dictVal) {
				return selectDictLabel(dictData, dictVal);
			}
		}
	}
</script>


<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
	
	.cell-pd {
		padding: 22rpx 10rpx;
	}

	.list-pd {
		margin-top: 10rpx;
	}
	
	/* 上传 */
	.uni-uploader {
		flex: 1;
		flex-direction: column;
	}
	.uni-uploader-head {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	.uni-uploader-info {
		color: #B2B2B2;
	}
	.uni-uploader-body {
		margin-top: 16rpx;
	}
	.uni-uploader__files {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.uni-uploader__file {
		margin: 10rpx;
		width: 210rpx;
		height: 210rpx;
	}
	.uni-uploader__img {
		display: block;
		width: 210rpx;
		height: 210rpx;
	}
	.uni-uploader__input-box {
		position: relative;
		margin:10rpx;
		width: 208rpx;
		height: 208rpx;
		border: 2rpx solid #D9D9D9;
	}
	.uni-uploader__input-box:before,
	.uni-uploader__input-box:after {
		content: " ";
		position: absolute;
		top: 50%;
		left: 50%;
		-webkit-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
		background-color: #D9D9D9;
	}
	.uni-uploader__input-box:before {
		width: 4rpx;
		height: 79rpx;
	}
	.uni-uploader__input-box:after {
		width: 79rpx;
		height: 4rpx;
	}
	.uni-uploader__input-box:active {
		border-color: #999999;
	}
	.uni-uploader__input-box:active:before,
	.uni-uploader__input-box:active:after {
		background-color: #999999;
	}
	.uni-uploader__input {
		position: absolute;
		z-index: 1;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>